from django.db import models
from django.contrib.auth.models import User
from enum import Enum


class TicketStatus(models.TextChoices):
    PENDING = 'pending', '待处理'
    AWAITING_RESPONSE = 'awaiting_response', '待回复'
    ASSISTED = 'assisted', '已协助'
    ASSISTANCE_COMPLETED = 'assistance_completed', '协助已完成'
    ASSISTANCE_REJECTED = 'assistance_rejected', '协助退回'
    CLOSED = 'closed', '完成关闭'


class Ticket(models.Model):
    title = models.CharField(max_length=200, verbose_name='工单标题')
    description = models.TextField(verbose_name='工单描述')
    creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_tickets', verbose_name='创建人')
    current_status = models.CharField(
        max_length=20,
        choices=TicketStatus.choices,
        default=TicketStatus.PENDING,
        verbose_name='当前状态'
    )
    current_handler = models.ForeignKey(
        User, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True, 
        related_name='handled_tickets', 
        verbose_name='当前处理人'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '工单'
        verbose_name_plural = '工单'

    def __str__(self):
        return f'{self.title} ({self.get_current_status_display()})'


class TicketAssignment(models.Model):
    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE, related_name='assignments', verbose_name='工单')
    handler = models.ForeignKey(User, on_delete=models.CASCADE, related_name='ticket_assignments', verbose_name='处理人')
    assigned_at = models.DateTimeField(auto_now_add=True, verbose_name='分配时间')
    status_at_assignment = models.CharField(
        max_length=20,
        choices=TicketStatus.choices,
        verbose_name='分配时状态'
    )
    notes = models.TextField(blank=True, null=True, verbose_name='备注')

    class Meta:
        verbose_name = '工单分配'
        verbose_name_plural = '工单分配'
        ordering = ['-assigned_at']

    def __str__(self):
        return f'{self.ticket.title} - {self.handler.username} ({self.get_status_at_assignment_display()})'


class TicketAction(models.Model):
    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE, related_name='actions', verbose_name='工单')
    actor = models.ForeignKey(User, on_delete=models.CASCADE, related_name='ticket_actions', verbose_name='操作人')
    action_time = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')
    from_status = models.CharField(
        max_length=20,
        choices=TicketStatus.choices,
        verbose_name='从状态'
    )
    to_status = models.CharField(
        max_length=20,
        choices=TicketStatus.choices,
        verbose_name='到状态'
    )
    comment = models.TextField(blank=True, null=True, verbose_name='评论')
    next_handler = models.ForeignKey(
        User, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True, 
        related_name='next_ticket_actions', 
        verbose_name='下一处理人'
    )

    class Meta:
        verbose_name = '工单操作'
        verbose_name_plural = '工单操作'
        ordering = ['-action_time']

    def __str__(self):
        return f'{self.ticket.title} - {self.actor.username} ({self.get_from_status_display()} -> {self.get_to_status_display()})'
